package org.bonorsoft.modules.wechat.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.House;
import org.bonorsoft.modules.control.entity.Population;
import org.bonorsoft.modules.control.service.ICommonService;
import org.bonorsoft.modules.control.service.IHouseService;
import org.bonorsoft.modules.control.service.IInforService;
import org.bonorsoft.modules.control.service.IPopulationService;
import org.bonorsoft.modules.control.vo.PopulationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * 小程序端实有人口信息
 */
@Slf4j
@Controller
@RequestMapping("/wechat/population")
public class WxPopulationController extends BonorController<Population, IPopulationService> {

    @Autowired
    private IPopulationService populationService;

    @Autowired
    private ICommonService commonService;


    /**
     * 实有人口分页列表查询
     *
     * @param population
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "实有人口信息-分页列表查询")
    @RequiresPermissions("wechat:population:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(Population population,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Population> queryWrapper = QueryGenerator.initQueryWrapper(population, req.getParameterMap());
        Page<Population> page = new Page<Population>(pageNo, pageSize);
        IPage<PopulationVo> pageList = populationService.paging(page,queryWrapper);
        return Result.ok(pageList);
    }
    /**
     * 实有人口添加
     *
     * @param population
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> add(@RequestBody PopulationVo population) {
        String photo = population.getPhoto();
        String rkbm = UUID.randomUUID().toString().replaceAll("-", "");
        population.setRkbm(rkbm).setInTime(new Date()).setGxsj(new Date());
        populationService.save(population);
        if(oConvertUtils.isNotEmpty(photo)){
            commonService.addPhoto(rkbm, photo);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 实有人口详细
     * @param req
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Result<?> detail(HttpServletRequest req){
        String id = req.getParameter("id");
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        Population popu = populationService.getById(id);
        PopulationVo popuvo = new PopulationVo();
        BeanUtils.copyProperties(popu,popuvo);
        popuvo.setPhoto(commonService.getPhoto(id));
        obj.put("detail",popuvo);
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }

//    /**
//     * 实有人口详细(根据地址)
//     * @param req
//     * @return
//     */
//    @GetMapping("/detail")
//    @ResponseBody
//    public Result<?> detailShow(HttpServletRequest req){
//        String id = req.getParameter("id");
//        Result<JSONObject> result = new Result<>();
//        JSONObject obj = new JSONObject();
//        obj.put("detail",populationService.listing(new QueryWrapper<Population>().eq("dzbm", id)));
//        result.setResult(obj);
//        result.success("查询成功！");
//        return result;
//    }

    /**
     * 实有人口编辑
     *
     * @param population
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> edit(@RequestBody PopulationVo population) {
        String photo = population.getPhoto();
        System.out.println(photo);
        population.setGxsj(new Date());
        populationService.updateById(population);
        if(oConvertUtils.isNotEmpty(photo)){
            commonService.updatePhoto(population.getRkbm(), photo);
        }
        return Result.ok("编辑成功!");
    }

}
